package smlab.loader
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import smlab.view.ui.UIComponent_SM;
	
	/**
	 * 显示 Loading 的面板
	 * @author simon
	 * 
	 */
	public class LoadingPanel_SM extends smlab.view.ui.UIComponent_SM
	{
		private const BAR:String="bar";
		private const TXT:String="txt";
		private const CLOSEBTN:String="closeBtn";
		private const CANCELBTN:String="cancelBtn";
		private const SUBMITBTN:String="submitBtn";
		
		private var _instance:Sprite;//进度条
		private var _bar:Sprite;//进度条
		private var _barWidth:Number;//进度条原始高度
		private var _barheight:Number;//进度条原始宽度
		private var _txt:TextField;//文本
		private var _closeBtn:SimpleButton;//关闭按钮（X按钮）
		private var _cancelBtn:SimpleButton;//取消按钮
		private var _submitBtn:SimpleButton;//确定按钮
		private var _closeFun:Function;//关闭按钮方法
		private var _cancelFun:Function;//取消按钮方法
		private var _submitFun:Function;//确定按钮方法
		private var _defaultMsg:String="";//默认显示文本
		private var _isShowLoading:Boolean=true;//是否显示loading条
		private var _showPercentStr:Boolean=true;//是否在 loading 文本上显示百分比
		
		private static var _instance:LoadingPanel_SM;
		
		/**
		 * 构造函数
		 * @param $skin		Loading 的皮肤
		 * @param cancelFun	取消按钮触发的方法
		 * @param submitFun	确定（提交）按钮触发的方法
		 * @param closeFun	关闭面板触发的方法
		 * 
		 */
		public function LoadingPanel_SM($skin:Sprite,$submitFun:Function=null,$cancelFun:Function=null,$closeFun:Function=null,$showPercentStr:Boolean=true)
		{
			super();
			closeFun=$closeFun;
			cancelFun=$cancelFun;
			submitFun=$submitFun;
			showPercentStr=$showPercentStr;
			this.skin=$skin;
		}
		
		public static function getInstance():LoadingPanel_SM
		{
			if(!_instance){
				_instance=new LoadingPanel_SM(null);
			}
			return _instance;
		}
		
		//设置按钮皮肤
		public function set closeBtn(b:SimpleButton):void{
			removeListener();
			_closeBtn=b;
			addListener();
		}
		
		public function get closeBtn():SimpleButton{
			return _closeBtn;
		}
		
		public function set cancelBtn(b:SimpleButton):void{
			removeListener();
			_cancelBtn=b;
			addListener();
		}
		
		public function get cancelBtn():SimpleButton{
			return _cancelBtn;
		}
		
		public function set submitBtn(b:SimpleButton):void{
			removeListener();
			_submitBtn=b;
			addListener();
		}
		
		public function get submitBtn():SimpleButton{
			return _submitBtn;
		}
		
		public function set defaultMsg(s:String):void{
			_defaultMsg=s;
		}
		
		public function get defaultMsg():String{
			return _defaultMsg;
		}
		
		//设置按钮事件所有响应方法
		public function set closeFun(f:Function):void{
			_closeFun=f;
		}
		
		public function get closeFun():Function{
			return _closeFun;
		}
		
		public function set cancelFun(f:Function):void{
			_cancelFun=f;
		}
		
		public function get cancelFun():Function{
			return _cancelFun;
		}
		
		public function set submitFun(f:Function):void{
			_submitFun=f;
		}
		
		public function get submitFun():Function{
			return _submitFun;
		}
		
		/**
		 * 是否在 loading 文本上显示百分比
		 * @param b
		 * 
		 */
		public function set showPercentStr(b:Boolean):void{
			_showPercentStr=b;
		}
		
		public function get showPercentStr():Boolean{
			return _showPercentStr;
		}
		
		/**
		 * 是否在文本上显示 loading
		 * @return 
		 * 
		 */
		public function get isShowLoading():Boolean{
			return _isShowLoading;
		}
		
		public function set isShowLoading(b:Boolean):void{
			_isShowLoading=b;
			if(b){
				showLoading();
			}else{
				this.hide();
			}
		}
		
		/**
		 * 设置、重新设置皮肤
		 * @param d
		 * 
		 */
		override public function set skin(d:DisplayObject):void
		{
			if(!d){
				return;
			}
			
			removeListener();
			super.skin=d;
			
			_bar=DisplayObjectContainer(skin).getChildByName(BAR) as Sprite;
			
			if(_bar)
			{
				_barWidth=_bar.width;
				_barheight=_bar.height;
				var r:Rectangle=new Rectangle(0,0,0,_barheight);
				_bar.scrollRect=r;
			}
			
			_txt=DisplayObjectContainer(skin).getChildByName(TXT) as TextField;
			
			if(_txt){
				_txt.mouseEnabled=false;
			}
			
			closeBtn=DisplayObjectContainer(skin).getChildByName(CLOSEBTN) as SimpleButton;
			cancelBtn=DisplayObjectContainer(skin).getChildByName(CANCELBTN) as SimpleButton;
			submitBtn=DisplayObjectContainer(skin).getChildByName(SUBMITBTN) as SimpleButton;
			addListener();
		}
		
		private function removeListener():void
		{
			if(closeBtn)closeBtn.removeEventListener(MouseEvent.CLICK,clickHandler);
			if(cancelBtn)cancelBtn.removeEventListener(MouseEvent.CLICK,clickHandler);
			if(submitBtn)submitBtn.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		private function addListener():void
		{
			if(closeBtn)closeBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			if(cancelBtn)cancelBtn.addEventListener(MouseEvent.CLICK,clickHandler);
			if(submitBtn)submitBtn.addEventListener(MouseEvent.CLICK,clickHandler);
		}
		
		public function showLoading(byteLoaded:Number=0,byteTotal:Number=0,txt:String=""):void
		{
			var s:String;
			
			if(txt==""){
				s=defaultMsg;
			}else{
				s=txt;
			}
			
			if(isShowLoading)
			{
				if(_txt)
				{
					if(byteTotal>0 && showPercentStr){
						_txt.text=s+int(byteLoaded/byteTotal*100)+"%";
					}else{
						_txt.text=s;
					}
				}
				
				if(_bar!=null && byteTotal>0){
					var w:Number=_barWidth*byteLoaded/byteTotal;
					var r:Rectangle=new Rectangle(0,0,w,_barheight);
					_bar.scrollRect=r;
				}
				
				super.show();
			}
		}
		
		private function clickHandler(e:MouseEvent):void
		{
			switch(e.currentTarget){
				case _closeBtn:
					if(closeFun!=null)_closeFun();
					break;
				case _cancelBtn:
					if(cancelFun!=null)_cancelFun();
					break;
				case _submitBtn:
					if(submitFun!=null)_submitFun();
					break;
			}
		}
		
		override public function show():void
		{
			throw new Error("您应该调用 LoadingPanel_SM 的 showLoading 方法而不是 show");
		}
		
	}
}